package com.yipinhouse.common.result;

import com.yipinhouse.common.resultcode.ResultCode;

import java.util.List;
import java.util.Set;

public class Result<T> {
    /**
     * 相应码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public static Result<?> getInstance(){
        Result<?> result = new Result<>();
        result.setCode(CommonResultCode.SUCCESS.getCode());
        result.setMsg(CommonResultCode.SUCCESS.getMessage());
        return result;
    }

    public static Result<?> getInstance(ResultCode resultCode){
        Result<?> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMessage());
        return result;
    }

    public static <S> Result<S> getInstance(Class<S> type){
        Result<S> result = new Result<>();
        result.setCode(CommonResultCode.SUCCESS.getCode());
        result.setMsg(CommonResultCode.SUCCESS.getMessage());
        return result;
    }

    public static <S> Result<S> getInstance(Class<S> type,ResultCode resultCode){
        Result<S> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMessage());
        return result;
    }

    public static <S> Result<List<S>> getInstanceList(Class<S> type){
        Result<List<S>> result = new Result<List<S>>();
        result.setCode(CommonResultCode.SUCCESS.getCode());
        result.setMsg(CommonResultCode.SUCCESS.getMessage());
        return result;
    }

    public static <S> Result<Set<S>> getInstanceSet(Class<S> type){
        Result<Set<S>> result = new Result<Set<S>>();
        result.setCode(CommonResultCode.SUCCESS.getCode());
        result.setMsg(CommonResultCode.SUCCESS.getMessage());
        return result;
    }

    public boolean isSuccess(){
        return code == CommonResultCode.SUCCESS.getCode();
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }
}
